//
//  HEAITabBar.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/1/18.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEAITabBar.h"

@interface HEAITabBar ()

@property (nonatomic, strong) UIButton *middleBtn;

@property (nonatomic, strong) UIButton *sendBtn;

@property (nonatomic,assign)UIEdgeInsets oldSafeAreaInsets;

@end

@implementation HEAITabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self customTabbar];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self addSubview:self.sendBtn];
     
     
    // 其他位置按钮
    NSUInteger count = self.subviews.count;
    for (NSUInteger i = 0 , j = 0; i < count; i++)
    {
        UIView *view = self.subviews[i];
        Class class = NSClassFromString(@"UITabBarButton");
        if ([view isKindOfClass:class])
        {
            view.zx_width = self.zx_width / 5.0;
            view.zx_x = self.zx_width * j / 5.0;
            j++;
            if (j == 2)
            {
                j++;
            }
        }
    }
}


- (void)customTabbar {
    
    self.translucent = NO;
    self.backgroundColor = KWhiteColor;
    
    // 去除顶部横线
    if (@available(iOS 13.0, *)) {
        [self.standardAppearance setBackgroundColor:[UIColor whiteColor]];
        [self.standardAppearance setBackgroundImage: [self imageWithColor: KClearColor]];
        
        [self.standardAppearance setShadowImage: [self imageWithColor: CSplitLineColor]];
    } else {
        [self setBackgroundColor:[UIColor whiteColor]];
        [self setBackgroundImage: [self imageWithColor: KClearColor]];
        
        [self setShadowImage: [self imageWithColor: CSplitLineColor]];
    }
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    
}

- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (void) safeAreaInsetsDidChange
{
    [super safeAreaInsetsDidChange];
    if(self.oldSafeAreaInsets.left != self.safeAreaInsets.left ||
       self.oldSafeAreaInsets.right != self.safeAreaInsets.right ||
       self.oldSafeAreaInsets.top != self.safeAreaInsets.top ||
       self.oldSafeAreaInsets.bottom != self.safeAreaInsets.bottom)
    {
        self.oldSafeAreaInsets = self.safeAreaInsets;
        [self invalidateIntrinsicContentSize];
        [self.superview setNeedsLayout];
        [self.superview layoutSubviews];
    }
    
}

- (CGSize) sizeThatFits:(CGSize) size
{
    CGSize s = [super sizeThatFits:size];
    if(@available(iOS 11.0, *))
    {
        CGFloat bottomInset = self.safeAreaInsets.bottom;
        if( bottomInset > 0 && s.height < 50) {
            s.height += bottomInset;
        }
    }
    return s;
}

- (void)centerButtonClick:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(tabBarCenterButtonClick:)]) {
        [self.delegate tabBarCenterButtonClick:self];
    }
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (self.isHidden == NO)
    {
        CGPoint newP = [self convertPoint:point toView:self.middleBtn];
        if ( [self.middleBtn pointInside:newP withEvent:event])
        {
            return self.middleBtn;
        }else
        {
            return [super hitTest:point withEvent:event];
        }
    }
    else
    {
        return [super hitTest:point withEvent:event];
    }
}

- (UIButton *)sendBtn{
    if (!_sendBtn) {
        _sendBtn = [[UIButton alloc] init];
        [_sendBtn setBackgroundImage:IMAGE_NAMED(@"home_ai") forState:UIControlStateNormal];
        _sendBtn.titleLabel.font = [UIFont systemFontOfSize:10];
        [_sendBtn addTarget:self action:@selector(centerButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _sendBtn.adjustsImageWhenHighlighted = NO;
        _sendBtn.size = CGSizeMake(50, 50);
        
        _sendBtn.layer.masksToBounds = YES;
        _sendBtn.layer.cornerRadius = 50 / 2.0;
        
        _sendBtn.zx_centerX = self.zx_width / 2;
        _sendBtn.zx_centerY = 12;
        [_sendBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.middleBtn = _sendBtn;
    }
    return _sendBtn;
}

#pragma mark -  -----------------以下两个方法解决ios12.1tabbar图标位移问题，如以后IOS12.1解决则可移除--------------

/**
 *  用 block 重写某个 class 的指定方法
 *  @param targetClass 要重写的 class
 *  @param targetSelector 要重写的 class 里的实例方法，注意如果该方法不存在于 targetClass 里，则什么都不做
 *  @param implementationBlock 该 block 必须返回一个 block，返回的 block 将被当成 targetSelector 的新实现，所以要在内部自己处理对 super 的调用，以及对当前调用方法的 self 的 class 的保护判断（因为如果 targetClass 的 targetSelector 是继承自父类的，targetClass 内部并没有重写这个方法，则我们这个函数最终重写的其实是父类的 targetSelector，所以会产生预期之外的 class 的影响，例如 targetClass 传进来  UIButton.class，则最终可能会影响到 UIView.class），implementationBlock 的参数里第一个为你要修改的 class，也即等同于 targetClass，第二个参数为你要修改的 selector，也即等同于 targetSelector，第三个参数是 targetSelector 原本的实现，由于 IMP 可以直接当成 C 函数调用，所以可利用它来实现“调用 super”的效果，但由于 targetSelector 的参数个数、参数类型、返回值类型，都会影响 IMP 的调用写法，所以这个调用只能由业务自己写。
 */
//CG_INLINE BOOL
//OverrideImplementation(Class targetClass, SEL targetSelector, id (^implementationBlock)(Class originClass, SEL originCMD, IMP originIMP)) {
//    Method originMethod = class_getInstanceMethod(targetClass, targetSelector);
//    if (!originMethod) {
//        return NO;
//    }
//    IMP originIMP = method_getImplementation(originMethod);
//    method_setImplementation(originMethod, imp_implementationWithBlock(implementationBlock(targetClass, targetSelector, originIMP)));
//    return YES;
//}
//
//+ (void)load {
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        if (@available(iOS 12.1, *)) {
//            OverrideImplementation(NSClassFromString(@"UITabBarButton"), @selector(setFrame:), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP originIMP) {
//                return ^(UIView *selfObject, CGRect firstArgv) {
//                    
//                    if ([selfObject isKindOfClass:originClass]) {
//                        // 如果发现即将要设置一个 size 为空的 frame，则屏蔽掉本次设置
//                        if (!CGRectIsEmpty(selfObject.frame) && CGRectIsEmpty(firstArgv)) {
//                            return;
//                        }
//                    }
//                    
//                    // call super
//                    void (*originSelectorIMP)(id, SEL, CGRect);
//                    originSelectorIMP = (void (*)(id, SEL, CGRect))originIMP;
//                    originSelectorIMP(selfObject, originCMD, firstArgv);
//                };
//            });
//        }
//    });
//}

@end

